<!-- UserModel.php-->
<?php 
	/*$basedir = dirname(dirname(dirname(__FILE__))).'/common/ConnDB.php';
	echo $basedir;*/
	/*require dirname(dirname(dirname(__FILE__))).'/common/ConnDB.php';
	require dirname(dirname(dirname(__FILE__))).'/common/config.php';*/
	require '../common/ConnDB.php';
	require '../common/config.php';
	//dirname(__FILE__) 一般会返回文件所在当前目录到系统根目录的一个目录结构。
	require 'Model.php';
	class UserModel extends Model{
		function __Construct(){
			parent::__Construct();
		}
		function getUserList(){
			$sql = 'select * from departmentusers';
			$data = $this->conn->query($sql);
			//$this->conn = new ConnDB();
			return $data;
		}

		function getUserByID($id){
			$sql = "select * from departmentusers where id=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}

		function getuserinfor($uname){
			$sql = "select * from departmentusers where name like '%".$uname."%' limit 0,5 ";;
			$data = $this->conn->query($sql);
			return $data;
		}

		function Updateuser($data){
			//var_dump($data);
			if(!empty($_FILES["photo"]["name"])){
  				$photo = parent::uploadfile('photo','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$photo='';
		  	}
		  	$user = array(
				'studentid' => $_POST['studentid'],  
				'name' => $_POST['name'],
				'sex' => $_POST['sex'],
				'department' => $_POST['department'],
				'xibie' => $_POST['xibie'],
				'photo'=>$photo,
				'intro'=>$_POST['intro']
				);
			$sql="update departmentusers set id='$data',studentid='$user[studentid]',name='$user[name]',sex='$user[sex]',department='$user[department]',xibie='$user[xibie]',photo='$user[photo]',intro='$user[intro]' where id=".$data;
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function addUser($data){
			//var_dump($data);
			//如果选择了头像，则上传头像文件
			if(!empty($_FILES["photo"]["name"])){
  				$photo = parent::uploadfile('photo','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$photo='';
		  	}
			$user = array(
				'id' =>'',
				'studentid' => $data['studentid'],
				'name' => $data['name'],
				'sex' => $data['sex'],
				'department' => $data['department'],
				'xibie' => $data['xibie'],
				'photo' => $photo,
				'intro' => $data['intro']
				);
			$keys = implode(',',array_keys($user));
			$values = implode("','",array_values($user));
			/*$sql = "insert into users values ('$user[uid]','$user[uname]','$user[password]','$user[gender]','$user[birthdate]','$user[hobby]','$user[degree]','$user[intro]','$user[pic]')";*/
			$sql="insert into departmentusers ({$keys}) VALUES ('{$values}')";
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function Deluser($id){
			$sql = "Delete from departmentusers where id=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}
	}
 ?>